<?php

namespace app\admin\controller\api;

use app\admin\library\DistinguishAddress;
use app\common\model;

/**
 * 单位
 * @internal
 */
class Company extends Api
{
    protected $noNeedLogin = ['getByCode'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 根据 code 获取信息
     *
     * @User ZhenZiHan
     * @Date 2020/3/11
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getByCode()
    {
        $code = $this->request->request('code', '');
        if (!$code) {
            $this->error('参数有误');
        }

        $school = \app\common\model\School::where([
                'code' => $code,
                'status' => \app\common\model\School::NORMAL
            ])
            ->field('id,pid,name,province_id,city_id,district_id,address,logo,note')
            ->find();
        if (!$school) {
            $this->error('数据不存在');
        }

        $this->success('', $school);
    }

    /**
     * 获取单位列表
     *
     * @User ZhenZiHan
     * @Date 2020/3/11
     *
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function lists()
    {
        $list = \app\common\model\School::whereIn('id', $this->getSchoolIds())
            ->field('id,pid,name,province_id,city_id,district_id,address,logo,code,note')
            ->select();
        $this->success('', $list);
    }
}